/*
 * BasicMockTazioTransaction.java    ver   24/ott/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.mock;

import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import org.mentalsmash.tazio.transactions.control.Snapshooter;
import org.mentalsmash.tazio.transactions.control.SystemTransactionManager;
import org.mentalsmash.tazio.transactions.control.impl.TazioTransactionImpl;
import org.mentalsmash.tazio.transactions.model.identifiers.TID;
import org.mentalsmash.tazio.transactions.user.TransactionStatus;

/**
 * Mock TazioTransaction that practically does nothig.
 * <p>
 * It starts in the <i>RUNNING</i> status at creation time (no need to call
 * <code>start()</code> on any transaction manager, and it does nothing else
 * then changing its status to <i>COMMITTED</i> or <i>ROLLED_BACK</i> when
 * {@link #commit()} or {@link #rollback()} are called respectively
 * </p>
 * 
 * @version 0.0.1 24/ott/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
public class BasicMockTazioTransaction extends TazioTransactionImpl {

    private static final long serialVersionUID = -1908810526739354316L;

    private static final AtomicInteger commitCounter = new AtomicInteger(0);

    public BasicMockTazioTransaction(final SystemTransactionManager tm,
	    final Snapshooter snapshooter, final TID tid) {
	super(tm, snapshooter, tid);
	this.setStatus(TransactionStatus.RUNNING);
	this.setSeqNo(commitCounter.get());
    }

    /**
     * This mock implementation simply changes the status of the transaction to
     * {@link TransactionStatus#COMMITTED}
     */
    @Override
    public void commit() {
	this.stateTransition(TransactionStatus.COMMITTED);
	this.setSeqNo(commitCounter.getAndIncrement());
    }

    /**
     * Changes the status of the transaction to
     * {@link TransactionStatus#COMMITTED} and returns <code>null</code>
     */
    @Override
    public Future<TransactionStatus> commitAsync() {
	this.stateTransition(TransactionStatus.COMMITTED);
	this.setSeqNo(commitCounter.getAndIncrement());
	return null;
    }

    /**
     * Sets the status of the transaction to
     * {@link TransactionStatus#ROLLED_BACK} and returns
     */
    @Override
    public void rollback() {
	this.stateTransition(TransactionStatus.ROLLED_BACK);

    }

    /**
     * Sets the status of the transaction to
     * {@link TransactionStatus#ROLLED_BACK} and returns
     */
    @Override
    public void setRollbackOnly() {
	this.stateTransition(TransactionStatus.ROLLED_BACK);

    }

    public void setToGlobalValidationSate() {
	this.stateTransition(TransactionStatus.GLOBAL_VALIDATION);
    }

}
